﻿using FXSStation.Modules.SharedInfrastructureModule.Exceptions;
using System.ComponentModel;
using System;

namespace FXSStation.Modules.SharedInfrastructureModule.BusinessClasses.Accounts
{
    [Serializable]
    public class Balance : INotifyPropertyChanged
    {
        private decimal _initialDeposit;
        private decimal _deposit;
        public decimal InitialDeposit
        {
            get
            {
                return _initialDeposit;
            }
            private set
            {
                _initialDeposit = value;
            }
        }
        public decimal Deposit
        {
            get { return _deposit; }
            private set 
            {
                if (_deposit != value)
                {
                    _deposit = value;
                    RaisePropertyChanged("Deposit");
                }
            }
        }

        public void Add(decimal sum)
        {
            this.Deposit += sum;
        }

        public void Subtract(decimal sum)
        {
            if (this.Deposit > sum)
            {
                this.Deposit -= sum;
            }
            else
            {
                throw new InsufficientBallanceException();
            }
        }

        public Balance(decimal initialDeposit)
        {
            this._initialDeposit = initialDeposit;
            this._deposit = initialDeposit;
        }

        public override string ToString()
        {
            return this.Deposit.ToString("C");
        }

        #region INotifyPropertyChanged Members
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
